package com.shu.exception;

import com.shu.domain.po.ResultMessage;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * Created by shu on 2017/8/15.
 */
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    @ExceptionHandler(CustomException.class)
    public ResultMessage customExceptionHandler(HttpServletResponse response, Exception ex) throws Exception{
        ResultMessage resultMessage = new ResultMessage();
        CustomException customException = (CustomException)ex;
        response.setStatus(customException.getCode().intValue());
        resultMessage.setCode(customException.getCode());
        resultMessage.setMsg(customException.getMsg());
        return resultMessage;
    }

//    @ExceptionHandler
//    public ResultMessage globalExceptionHandler(HttpServletResponse response, Exception ex) throws Exception {
//        ResultMessage resultMessage = new ResultMessage();
//        ex.printStackTrace();
//        response.setStatus(600);
//        resultMessage.setCode(600);
//        resultMessage.setMsg("系统异常");
//        return resultMessage;
//    }

}
